package com.culture.config.wrapper;

import java.util.*;
import java.util.stream.Collectors;

import com.culture.config.dao.entity.UserRole;
import com.culture.config.model.po.*;
import com.culture.config.util.AuthenticationUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class UserRoleWrapper {

    public static UserRole wrapUserRoleQuery(UserRolePageParam param) {
		UserRole userRole = new UserRole();	  
		userRole.setRoleId(param.getRoleId()).setUserId(param.getUserId());
		if(StringUtils.isNotBlank(param.getRoleName())){
			userRole.setRoleName(param.getRoleName());
		}	
		if(StringUtils.isNotBlank(param.getUserName())){
			userRole.setUserName(param.getUserName());
		}		
		return userRole;
	}		

	public static List<UserRole> wrapUserRoleListAdd(UserRoleUpdatePO userRoleUpdatePO){		
		if(CollectionUtils.isEmpty(userRoleUpdatePO.getRoleList())){
			return Collections.emptyList();
		}
		Set<Long> roleIds = userRoleUpdatePO.getRoleList().stream().collect(Collectors.toSet());
		List<UserRole> userRoles = new ArrayList<>(roleIds.size());
		roleIds.forEach(e->{
			UserRole userRole = new UserRole(userRoleUpdatePO.getUserId());
			userRole.setRoleId(e);
			userRole.setDeleted(Optional.ofNullable(userRoleUpdatePO.getStatus()).orElse(0));			
			AuthenticationUtil.setInsertFieldInfo(userRole);
			userRoles.add(userRole);
		});
		return userRoles;
	}
	
}
